'use strict'
app.controller('fullReductionAddCtrl', ['$scope', 'utilService', '$filter', 'fullReductionAddService', 'toaster', '$state', '$stateParams', '$modal', function ($scope, utilService, $filter, fullReductionAddService, toaster, $state, $stateParams, $modal) {
    $scope.fullReductionVO = {
        startTime: $filter('date')(new Date(), 'yyyy-MM-dd'),
        endTime: $filter('date')(new Date(), 'yyyy-MM-dd'),
        type:0
    }
    //获取优惠券
    $scope.queryForCoupon = function () {
        fullReductionAddService.queryForCoupon($scope).then(
            function (rlt) {
                switch (rlt.status) {
                    case 200:
                        $scope.couponsList = rlt.data;
                        $scope.pushCondition();
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            }
        );
    }
    $scope.queryForCoupon();

    $scope.fullReductionConditionJson = new Array();
    //存储条件对象
    $scope.pushCondition = function () {
        var obj = new Object();
        obj.level = $scope.fullReductionConditionJson.length + 1;
        obj.isOpenDecreaseCash = 0;
        obj.decreaseCash = 0.00;
        obj.isOpenDiscount = 0;
        obj.discount = 0.0;
        obj.isOpenPostageFree = 0;
        //obj.isOpenPoints = 0;
        //obj.points = 0;
        obj.isOpenCoupon = 0;
        obj.couponCount = 1;
        $scope.fullReductionConditionJson.push(obj);
    }

    //删除条件对象
    $scope.popCondition = function (level) {
        $scope.fullReductionConditionJson.splice(level - 1, 1);
        if (level < $scope.fullReductionConditionJson.length + 1) {
            for (var i = level - 1; $scope.fullReductionConditionJson.length; i++) {
                $scope.fullReductionConditionJson[i].level = $scope.fullReductionConditionJson[i].level - 1;
            }
        }
    }

    $scope.activitiesType = 0;
    $scope.fullReductionGoodsVO = {
        goodsIdArr: []
    };
    //获取店铺所有商品
    $scope.queryForGoods = function () {
        $scope.goodsList={};
        $scope.fullReductionVO.startTime = $filter('date')($scope.fullReductionVO.startTime, 'yyyy-MM-dd');
        $scope.fullReductionVO.endTime = $filter('date')($scope.fullReductionVO.endTime, 'yyyy-MM-dd');
        fullReductionAddService.queryForGoods($scope).then(
            function (rlt) {
                switch (rlt.status) {
                    case 200:
                        if (rlt.data.length > 0) {
                            $scope.goodsList = rlt.data;
                            $scope.changeActivitiesType($scope.activitiesType);
                        }
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break;
                }
            }
        );
    }
    $scope.queryForGoods();
    //活动商品选择
    $scope.changeActivitiesType = function (type) {
        $scope.fullReductionGoodsVO.goodsIdArr.splice(0, $scope.fullReductionGoodsVO.goodsIdArr.length);
        if ($scope.goodsList.length < 1) {
            return;
        }
        if (type == 0) {
            angular.forEach($scope.goodsList, function (item) {
                item.isChecked = 1;
                $scope.fullReductionGoodsVO.goodsIdArr.push(item.id);
            })
        }
        if (type == 1) {
            angular.forEach($scope.goodsList, function (item) {
                if (item.isChecked == 1) {
                    $scope.fullReductionGoodsVO.goodsIdArr.push(item.id);
                }
            })
            //选择部分商品模态框
            var modalInstance = $modal.open({
                templateUrl: 'tpl/marketingManager/choose_part_goods_popup.html',
                controller: 'choosePartGoodsPopupCtrl',
                size: 'md',
                scope: $scope
            });

            modalInstance.result.then(function (selectedItem) {
                $scope.selected = selectedItem;
            }, function () {
                console.info('Modal dismissed at: ' + new Date());
            });
        }
    }

    $scope.saveStatus = false;
    //添加满减活动设置
    $scope.addFullReduction = function () {
        if($scope.saveStatus){
            return;
        }
        $scope.fullReductionVO.startTime = $filter('date')($scope.fullReductionVO.startTime, 'yyyy-MM-dd');
        $scope.fullReductionVO.endTime = $filter('date')($scope.fullReductionVO.endTime, 'yyyy-MM-dd');
        $scope.saveStatus = true;
        fullReductionAddService.addFullReduction($scope).then(function (rlt) {
            switch (rlt.status) {
                case 200:
                    $scope.saveStatus = false;
                    toaster.pop('success', "提示", "操作成功");
                    $state.go('app.fullReduction');
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }
}])
app.controller('choosePartGoodsPopupCtrl', ['$scope', '$modalInstance', function ($scope, $modalInstance) {
    //选择商品
    $scope.checkedGoods = function (e, item) {
        if (e.target.checked) {
            item.isChecked = 1;
            $scope.fullReductionGoodsVO.goodsIdArr.push(item.id);
        } else {
            item.isChecked = 0;
            var idx = $scope.fullReductionGoodsVO.goodsIdArr.indexOf(item.id);
            $scope.fullReductionGoodsVO.goodsIdArr.splice(idx, 1);
        }
    }
    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
}])